/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package brigal.fastbackup.gui;

import brigal.fastbackup.data.Profile;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author brigal
 */
public class ProfileEditionEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {

    private JTable sourceTable;
    private int selectedRow;
    private int selectedColumn;
    private JButton button;
    protected final String EDIT = "edit";
    private ProfileEditionBox profileEditionBox;

    public ProfileEditionEditor() {
        button = new JButton();
        button.setText("...");
        button.setActionCommand(EDIT);
        button.addActionListener(this);
    }

    public Component getTableCellEditorComponent(
            JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column) {
        sourceTable = table;
        selectedRow = row;
        selectedColumn = column;
        return button;
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue() {
        return null;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(EDIT)) {
            String name = (String) sourceTable.getValueAt(selectedRow, 0);
            String source = (String) sourceTable.getValueAt(selectedRow, 1);
            String destination = (String) sourceTable.getValueAt(selectedRow, 2);
            Profile profile = new Profile(name, source, destination);

            JFrame mainFrame = FastBackupApp.getApplication().getMainFrame();
            profileEditionBox = new ProfileEditionBox(mainFrame, true, profile);
            profileEditionBox.setLocationRelativeTo(mainFrame);
            FastBackupApp.getApplication().show(profileEditionBox);

            profile = profileEditionBox.getEditedProfile();
            sourceTable.getModel().setValueAt(profile.getName(), selectedRow, 0);
            sourceTable.getModel().setValueAt(profile.getSource(), selectedRow, 1);
            sourceTable.getModel().setValueAt(profile.getDestination(), selectedRow, 2);
        }
    }
}

